package cn.xfdzx.android.apps.shop.activity;


import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.AddAddressBeen;
import cn.xfdzx.android.apps.shop.bean.AddressListBeen;
import cn.xfdzx.android.apps.shop.contract.AddAddressContract;
import cn.xfdzx.android.apps.shop.presenter.AddAddressPresenter;
import cn.xfdzx.android.apps.shop.util.RegexUtil;
import cn.xfdzx.android.apps.shop.util.StatusBarUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;
import static cn.xfdzx.android.apps.shop.apps.Constants.START_CODE;

/**
 * @author gzk
 * @data 2020/03/26
 * 新增地址
 */
public class AddAddressActivity extends BaseMvpActivity<AddAddressPresenter> implements AddAddressContract.View {
    @BindView(R.id.et_add_address_house_number)
    EditText tvnumber;//门牌号
    @BindView(R.id.et_add_address_name)
    EditText tvname;//联系人
    @BindView(R.id.et_add_address_phone_number)
    EditText tvnphone;//手机号
    @BindView(R.id.rg_add_address)
    RadioGroup address_rg;//标签
    @BindView(R.id.rb_add_address_home)
    RadioButton rb_home;//家
    @BindView(R.id.rb_add_address_compay)
    RadioButton rb_compay;//公司
    @BindView(R.id.rb_add_address_school)
    RadioButton rb_school;//学校
    @BindView(R.id.address_save)
    Button bt_save;//保存
    @BindView(R.id.tv_address_name)
    TextView tvAddressName;//收货地址
    @BindView(R.id.ll_state)
    LinearLayout llState;
    @BindView(R.id.tv_add_address_title)
    TextView tvAddAddressTitle;

    private Dialog dialog;
    private RelativeLayout.LayoutParams item_bar;
    private String lng, lat, is_default;//是否设为默认0不默认 1为默认地址
    private String id;    //地址id 有值为修改 没值为新增
    private String label="";    //1家 2公司 3学校
    private String from;    //来自哪个界面
    private AddressListBeen.DataBean dataBean;
    private Activity mContext;


    @Override
    public int getLayoutId() {
        return R.layout.activity_add_address;
    }

    @Override
    public void initView() {
        initHigh();
        mPresenter = new AddAddressPresenter();
        mPresenter.attachView(this);
        mContext = this;
        from = getIntent().getStringExtra("from");
        if (null!=from){
        if (from.equals("address_list")||from.equals("confirm")&&!getIntent().getStringExtra("id").isEmpty()) {//来自地址列表，点击编辑过来的
            tvAddAddressTitle.setText("修改收货地址");
            dataBean = (AddressListBeen.DataBean) getIntent().getSerializableExtra("data");
            tvAddressName.setText(dataBean.getAddress_info());
            tvnumber.setText(dataBean.getHouse_number());
            tvname.setText(dataBean.getRealname());
            tvnphone.setText(dataBean.getMobile());
            label = dataBean.getLabel() + "";
            if (dataBean.getLabel() == 1) {
                rb_home.setChecked(true);
            } else if (dataBean.getLabel() == 2) {
                rb_compay.setChecked(true);
            } else if (dataBean.getLabel() == 3) {
                rb_school.setChecked(true);
            }
            is_default = dataBean.getIs_default() + "";
            id = dataBean.getId() + "";
            lng = dataBean.getLng();
            lat = dataBean.getLat();
        } else {
            is_default = getIntent().getStringExtra("is_default");
            id = getIntent().getStringExtra("id");

        }}

        //标签的选择
        address_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_add_address_home:
                        //选择家
                        label = "1";
                        break;
                    case R.id.rb_add_address_compay:
                        //选择公司
                        label = "2";
                        break;
                    case R.id.rb_add_address_school:
                        //选择学校
                        label = "3";
                        break;
                }
            }
        });

        dialog = Utils.addDialog(this, R.layout.dialog, R.string.are_you_sure_you_want_to_drop_this_edit, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                finish();
            }
        });
    }

    private void initHigh() {
        item_bar = (RelativeLayout.LayoutParams) llState.getLayoutParams();
        item_bar.height = StatusBarUtil.getStatusBarHeight(this);
        llState.setLayoutParams(item_bar);
    }

    @OnClick({R.id.iv_location_back, R.id.ll_add_address_location, R.id.address_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_location_back:
                if (tvAddressName.getText().length() != 0 | tvnumber.getText().toString().length() != 0) {
                    dialog.show();
                }else {
                    finish();
                }
                break;
            case R.id.ll_add_address_location:
                //跳转到选择收货地址
                Intent intent_moer_address = new Intent(this, MoreAddressMapActivity.class);
                startActivityForResult(intent_moer_address, START_CODE);
                break;
            case R.id.address_save:
                //点击保存
                check();
                    break;
        }
    }

    @SuppressLint("NewApi")
    private void check() {
        if (tvAddressName.getText().length() == 0) {
            Toast.makeText(this, "请选择收货地址", Toast.LENGTH_SHORT).show();
            return;
        } else if (tvnumber.getText().toString().isEmpty()) {
            Toast.makeText(this, "请输入详细地址", Toast.LENGTH_SHORT).show();
            return;
        } else if (tvname.getText().toString().isEmpty()) {
            Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
            return;
        } else if (tvnphone.getText().toString().isEmpty()) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!RegexUtil.isMobileExact(tvnphone.getText().toString())) {
            Toast.makeText(this, "请输入正确格式的手机号码", Toast.LENGTH_SHORT).show();
            return;
        }else {
            mPresenter.addAddres(tvname.getText().toString().trim(), tvnphone.getText().toString().trim(), lng, lat, is_default, tvAddressName.getText().toString().trim(), tvnumber.getText().toString().trim(), id, label);

        }
    }

    @Override
    protected void onDestroy() {
        if (dialog.isShowing()) {
            dialog.dismiss();

        }
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == START_CODE) {
                String addressName = data.getStringExtra("address_name");
                lng = data.getStringExtra("lontitude");
                lat = data.getStringExtra("latitude");
                if (null != addressName) {
                    tvAddressName.setText(addressName);
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (tvAddressName.getText().length() != 0 | tvnumber.getText().toString().length() != 0) {
            dialog.show();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onAddAddressSuccess(AddAddressBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (from.equals("confirm")) {
                Intent intent = new Intent();
                intent.putExtra("data", bean.getData());
                setResult(RESULT_OK, intent);
                finish();
            } else if (from.equals("management") | from.equals("address_list")) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            } else if (from.equals("first")) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }
}
